﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Iudicium.Busqueda
{
    /// <summary>
    /// Summary description for UtilitatiosBusqueda
    /// </summary>
    public class UtilitatiosBusqueda
    {
        public enum TipoDatoDb
        {
            Texto, TextoCompleto, Numero, Fecha, RangoFecha
        }

        public UtilitatiosBusqueda()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static string CrearQuery(TipoDatoDb type, string columnName, string exp)
        {
            switch (type)
            {
                case TipoDatoDb.Texto:
                    return " " + columnName + " LIKE '%" + exp + "%' ";
                case TipoDatoDb.TextoCompleto:
                    string query = " (CONTAINS(" + columnName + ", '(";
                    string[] tokens = exp.Split(' ');
                    if(tokens == null || tokens.Length <= 0)
                        return query + "FORMSOF (INFLECTIONAL, " + exp.Trim() + ")'))";
                    if (tokens.Length > 0 && !string.IsNullOrEmpty(tokens[0].Trim()))
                    {
                        query += "FORMSOF (INFLECTIONAL, " + tokens[0].Trim() + ")";
                    }
                    for (int i = 1; i < tokens.Length; i++)
                    {
                        string token = tokens[i].Trim();
                        if (string.IsNullOrEmpty(token))
                            continue;
                        query += " AND FORMSOF (INFLECTIONAL, " + tokens[i] + ")";
                    }
                    //'(FORMSOF (INFLECTIONAL, jose) and FORMSOF (INFLECTIONAL, gutierrez))')
                    query += ")')) ";
                    return query;
                case TipoDatoDb.Numero:
                    return " " + columnName + " = '" + exp + "'";
                case TipoDatoDb.Fecha:
                    return " " + columnName + " = '" + exp + "'";
                case TipoDatoDb.RangoFecha:
                    string[] dates = exp.Split(',');
                    return " " + columnName + " BETWEEN '" + dates[0] + "' AND '"  + dates[1] + "' ";
                default:
                    break;
            }
            return "";
        }


    }
}